﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireSiteBLL
    {
        #region 构造单例
        private FireSiteBLL() { }
        private static FireSiteBLL _instance;
        public static FireSiteBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireSiteBLL());
            }
        }
        #endregion

        public int AddFireSite(Firesite info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                ef.Firesite.Add(info);
                ef.SaveChanges();
                return info.SiteId;
            }
        }

        public List<Firesite> GetFireSiteByFireID(int fireId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Firesite.Where(p => p.FireId == fireId).OrderBy(o=>o.SiteId).ToList();
            }
        }

        public int UpdateFireSite(Firesite info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Firesite.Where(p => p.SiteId == info.SiteId).FirstOrDefault();
                if (exist != null)
                {
                    exist.FireId = info.FireId;
                    exist.Longitude = info.Longitude;
                    exist.Latitude = info.Latitude;
                    exist.Altitude = info.Altitude;
                    exist.RecordType = info.RecordType;
                    exist.Content = info.Content;
                    exist.TransDatetime = info.TransDatetime;
                    exist.UserId = info.UserId;
                    exist.Ip = info.Ip;
                    ef.SaveChanges();
                    return exist.SiteId;
                }
                return 0;
            }
        }

    }
}
